package com.zgc.web.action;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sun.net.httpserver.Authenticator.Success;
import com.zgc.domain.PageBean;
import com.zgc.domain.TeachSource;
import com.zgc.domain.User;
import com.zgc.service.IsourceInfoService;
import com.zgc.utils.ZGCContext;

@Controller
@Scope("prototype")
public class SourceInfoAction extends ActionSupport implements
		ModelDriven<TeachSource> {
	@Resource
	private IsourceInfoService isourceInfoService;

	private TeachSource teachSource = new TeachSource();
	
	private File photoFile;
	private String photoFileFileName;
	List<TeachSource> sourceList = new ArrayList<>();
	private PageBean pageBean;
	private int page = 1;
	private String methodName;
	private String speciality;
	
	
	public String getSpeciality() {
		return speciality;
	}
	public void setSpeciality(String speciality) {
		this.speciality = speciality;
	}
	public String getMethodName() {
		return methodName;
	}
	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}
	public PageBean getPageBean() {
		return pageBean;
	}
	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}

	private String keyWord;
	
	public String getKeyWord() {
		return keyWord;
	}
	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}
	public List<TeachSource> getSourceList() {
		return sourceList;
	}
	public void setSourceList(List<TeachSource> sourceList) {
		this.sourceList = sourceList;
	}
	public String saveSourseInfo() throws IOException {
		String basePath = "D:\\img";
		File newFile = new File(basePath, photoFileFileName);
		FileUtils.copyFile(photoFile, newFile);
		teachSource.setPhoto("/img/"+photoFileFileName);
		isourceInfoService.saveSourseInfo(teachSource);
		return SUCCESS;
	}
	public String qryAllSource_1() {
		String hql = " from TeachSource "; //查询语句    
		pageBean = isourceInfoService.queryForPage(5, page,hql);
		sourceList = pageBean.getList();
		methodName = "qryAllSource_1";
		return SUCCESS;
	}
	public String qryAllSource_2() {
		String hql = " from TeachSource "; //查询语句    
		pageBean = isourceInfoService.queryForPage(5, page,hql);
		sourceList = pageBean.getList();
		methodName = "qryAllSource_2";
		return "list";
	}
	public String qryBySpec() {
		speciality = ServletActionContext.getRequest().getParameter("speciality");
		speciality = speciality.replace("\"","");
		String hql = " from TeachSource where speciality = "+"'"+speciality+"'";
		System.out.println(speciality);
		pageBean = isourceInfoService.queryForPage(5, page,hql);
		sourceList = pageBean.getList();
		methodName = "qryBySpec";
		return SUCCESS;
	}
	public String qryByKey() {
//		String keyWord = ServletActionContext.getRequest().getParameter("keyWord");
		String hql = " from TeachSource where teacherNm like '%"+keyWord+"%' ";
		pageBean = isourceInfoService.queryForPage(5, page,hql);
		sourceList = pageBean.getList();
		methodName = "qryByKey";
		return SUCCESS;
	}
	public String updateSource() {
		isourceInfoService.updateSourById(teachSource);
		return "list";
	}
	public String editSource() {
		int sourceId = Integer.parseInt(ServletActionContext.getRequest().getParameter("sourceId"));
		teachSource = isourceInfoService.qrySourceById(sourceId);
		return SUCCESS;
	}
	public String qryTSourceById() {
		int sourceId = Integer.parseInt(ServletActionContext.getRequest().getParameter("sourceId"));
		User loginUser = ZGCContext.getLoginUser();
		if (loginUser !=null ) {
			teachSource = isourceInfoService.qrySourceById(sourceId);
			ServletActionContext.getContext().getValueStack().set("teachSource", teachSource);
			return SUCCESS;
		}else {
			return "login";
		}
		
	}
	public String delSourById() {
		int sourceId = Integer.parseInt(ServletActionContext.getRequest().getParameter("sourceId"));
		isourceInfoService.delSourById(sourceId);
		return SUCCESS;
	}
	public TeachSource getModel() {
		return teachSource;
	}

	public File getPhotoFile() {
		return photoFile;
	}

	public void setPhotoFile(File photoFile) {
		this.photoFile = photoFile;
	}

	public String getPhotoFileFileName() {
		return photoFileFileName;
	}

	public void setPhotoFileFileName(String photoFileFileName) {
		this.photoFileFileName = photoFileFileName;
	}

}
